package com.example.weather_news.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.weather_news.R;
import com.example.weather_news.adapter.Adapter_AddCity;
import com.example.weather_news.db.SaveCityInfo;

import org.litepal.crud.DataSupport;

import java.util.List;

public class WeatherSettingActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_weatherSet_back;
    private ListView listView_myCity;
    private ImageView iv_addCity;
    private Adapter_AddCity adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_setting);
        initView();

        adapter = new Adapter_AddCity(WeatherSettingActivity.this);
        listView_myCity.setAdapter(adapter);
        querySaveCity();
    }

    //查询已保存的城市的天气
    private void querySaveCity() {
        List<SaveCityInfo> list = DataSupport.findAll(SaveCityInfo.class);
        list.remove(0);
        adapter.setData(list);
    }

    public void delateCity(String cityName){
        int a=DataSupport.deleteAll(SaveCityInfo.class,"cityName=?",cityName);
        if (a>0){
            querySaveCity();
        }
    }

    private void initView() {
        iv_weatherSet_back = (ImageView) findViewById(R.id.iv_weatherSet_back);
        listView_myCity = (ListView) findViewById(R.id.listView_myCity);
        iv_addCity = (ImageView) findViewById(R.id.iv_addCity);

        iv_weatherSet_back.setOnClickListener(this);
        iv_addCity.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_weatherSet_back:
                finish();
                overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                break;
            case R.id.iv_addCity:
                startActivityForResult(new Intent(WeatherSettingActivity.this, QueryCityActivity
                        .class), 1);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1) {//resultcode 返回值为1，直接后台关闭
            finish();
        }
    }
}
